#include <iostream>  
  
using namespace std;  
  
struct ListNode {  
    int val;  
    ListNode *next;  
    ListNode(int x) : val(x), next(NULL) {}  
};  
  
ListNode* swapPairs(ListNode* head) {  
    if (head == NULL || head->next == NULL) {  
        return head;  
    }  
    ListNode *dummy = new ListNode(0);  
    dummy->next = head;  
    ListNode *curr = dummy;  
    while (curr->next != NULL && curr->next->next != NULL) {  
        ListNode *first = curr->next;  
        ListNode *second = curr->next->next;  
        first->next = second->next;  
        second->next = first;  
        curr->next = second;  
        curr = curr->next->next;  
    }  
    ListNode *result = dummy->next;  
    delete dummy;  
    return result;  
}  
  
void printList(ListNode* head) {  
    ListNode* curr = head;  
    while (curr != NULL) {  
        cout << curr->val << " ";  
        curr = curr->next;  
    }  
    cout << endl;  
}  
  
int main() {  
    ListNode* head = new ListNode(1);  
    head->next = new ListNode(2);  
    head->next->next = new ListNode(3);  
    head->next->next->next = new ListNode(4);  
    head->next->next->next->next = new ListNode(5);  
    head->next->next->next->next->next = new ListNode(6);  
      
    cout << "Original list: ";  
    printList(head);  
      
    head = swapPairs(head);  
      
    cout << "List after swapping pairs: ";  
    printList(head);  
      
    return 0;  
}